package cn.iocoder.yudao.module.trade.controller.app.brokerage;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.member.api.user.MemberUserApi;
import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO;
import cn.iocoder.yudao.module.trade.controller.admin.brokerage.vo.record.BrokerageRecordRespVO;
import cn.iocoder.yudao.module.trade.controller.app.brokerage.vo.log.AppBrokerageLogPageReqVO;
import cn.iocoder.yudao.module.trade.controller.app.brokerage.vo.log.AppBrokerageLogRespVO;
import cn.iocoder.yudao.module.trade.convert.brokerage.BrokerageRecordConvert;
import cn.iocoder.yudao.module.trade.dal.dataobject.brokerage.BrokerageRecordDO;
import cn.iocoder.yudao.module.trade.service.brokerage.BrokerageRecordService;
import cn.iocoder.yudao.module.wine.api.MemberOpenLogApi;
import cn.iocoder.yudao.module.wine.api.dto.MemberOpenLogRespDTO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.validation.annotation.Validated;

import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
import static cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils.getLoginUserId;

import cn.iocoder.yudao.module.trade.dal.dataobject.brokerage.BrokerageLogDO;
import cn.iocoder.yudao.module.trade.service.brokerage.BrokerageLogService;

import java.util.List;
import java.util.Map;
import java.util.Set;

@Tag(name = "用户 APP - 佣金操作日志")
@RestController
@RequestMapping("/trade/brokerage-log")
@Validated
public class AppBrokerageLogController {

    @Resource
    private BrokerageLogService brokerageLogService;

    @Resource
    private BrokerageRecordService brokerageRecordService;

    @Resource
    private MemberUserApi memberUserApi;

    @Resource
    private MemberOpenLogApi memberOpenLogApi;


    @DeleteMapping("/delete")
    @Operation(summary = "删除佣金操作日志")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteBrokerageLog(@RequestParam("id") Long id) {
        brokerageLogService.deleteBrokerageLog(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得佣金操作日志")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<AppBrokerageLogRespVO> getBrokerageLog(@RequestParam("id") Long id) {
        BrokerageLogDO brokerageLog = brokerageLogService.getBrokerageLog(id);
        return success(BeanUtils.toBean(brokerageLog, AppBrokerageLogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得佣金操作日志分页")
    public CommonResult<PageResult<AppBrokerageLogRespVO>> getBrokerageLogPage(@Valid AppBrokerageLogPageReqVO pageReqVO) {
        PageResult<BrokerageLogDO> pageResult = brokerageLogService.getAppBrokerageLogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AppBrokerageLogRespVO.class));
    }

    @GetMapping("/get-list")
    @Operation(summary = "根据佣金操作日志Id获取操作记录列表")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<List<BrokerageRecordRespVO>> getBrokerageLogRecordList(@RequestParam("id") Long id) {
        BrokerageLogDO brokerageLog = brokerageLogService.getBrokerageLog(id);
        if(brokerageLog == null) {
            return success(null);
        }
        List<Integer> ids = brokerageLog.getRecords();
        List<BrokerageRecordDO> list = brokerageRecordService.getBrokerageRecordListByIds(ids);

        // 查询用户信息
        Set<Long> userIds = convertSet(list, BrokerageRecordDO::getUserId);
        userIds.addAll(convertList(list, BrokerageRecordDO::getSourceUserId));
        Map<Long, MemberUserRespDTO> userMap = memberUserApi.getUserMap(userIds);
        //查询缴费信息
        Set<String> tradeNoIds = convertSet(list, BrokerageRecordDO::getBizId);
        Map<String, MemberOpenLogRespDTO> logMap = memberOpenLogApi.getMemberOpenLogMap(tradeNoIds);
        // 拼接数据
        List<BrokerageRecordRespVO> resultList = BrokerageRecordConvert.INSTANCE.convertPage(list, userMap, logMap);

        return success(resultList);
    }

}